package Guanliyuan;
import java.awt.event.*;
import java.sql.*;

import javax.swing.*;

import Util.DbUtil;
import Yonghu.Yonghuduan;

public class DateRentSum  extends JFrame implements ActionListener{
	JTextField field1,field2;
	Box box1,box2,box3,baseBox;
	JButton buttonOfReturn;
	Connection con = null;
	Statement stmt1 = null;
	Statement stmt2 = null;
	ResultSet rs1 = null;
	ResultSet rs2 = null;
	String sumrent;
	String countcar;
	public DateRentSum()
	{
		init();
		setVisible(true);
		setBounds(500, 200, 400, 200);
		setTitle("当天租车统计");
		setText();
	}
	
	void init() 
	{
		JLabel label1 = new JLabel("租车数：");
		JLabel label2 = new JLabel("总计租金：");
		field1 = new JTextField();
		field2 = new JTextField();
		buttonOfReturn = new JButton("返回");
		buttonOfReturn.addActionListener(this);
		
		box1 = Box.createHorizontalBox();
		box1.add(Box.createHorizontalStrut(8));
		box1.add(label1);
		box1.add(Box.createHorizontalStrut(8));
		box1.add(field1);
		box1.add(Box.createHorizontalStrut(8));
		
		box2 = Box.createHorizontalBox();
		box2.add(Box.createHorizontalStrut(8));
		box2.add(label2);
		box2.add(Box.createHorizontalStrut(8));
		box2.add(field2);
		box2.add(Box.createHorizontalStrut(8));
		
		box3 = Box.createHorizontalBox();
		box3.add(Box.createHorizontalStrut(8));
		box3.add(buttonOfReturn);
		box3.add(Box.createHorizontalStrut(8));
		
		baseBox = Box.createVerticalBox();
		baseBox.add(Box.createVerticalStrut(15));
		baseBox.add(box1);
		baseBox.add(Box.createVerticalStrut(10));
		baseBox.add(box2);
		baseBox.add(Box.createVerticalStrut(10));
		baseBox.add(box3);
		baseBox.add(Box.createVerticalStrut(10));
		
		add(baseBox);
	}
	public void connDB() { // 连接数据库
		try {
			Class.forName("com.mysql.jdbc.Driver");//注册驱动
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {//创建连接
			con = DriverManager.getConnection(DbUtil.dbUrlString, DbUtil.dbUser, DbUtil.dbpassword);
			
		} catch (SQLException e) {
			e.printStackTrace();
			
		}
		
	}

	public void closeDB() // 关闭连接
	{
		try {
			stmt1.close();
			stmt2.close();
			con.close();
		} 
		catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public void setText()//打开界面就把数据库中信息写入field
	{
		this.connDB();
		try {
			stmt1 = con.createStatement();
			String sql1 = "select count(*) from car_information where hire='是';";
			rs1 = stmt1.executeQuery(sql1);
			if(rs1.next())
			{
				sumrent = rs1.getString("count(*)");
				field1.setText(sumrent);
				field1.setEnabled(false);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			stmt2 = con.createStatement();
			String sql2 = "select sum(price) from car_information where hire='是';";
			rs2 = stmt2.executeQuery(sql2);
			if(rs2.next())
			{
				countcar = rs2.getString("sum(price)");
				field2.setText(countcar);
				field2.setEnabled(false);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if(source == buttonOfReturn)
		{
			this.dispose();
			new Guanliyuan(false);
		}
	}

}

	

